package com.boardgame.gwt.service;

import com.boardgame.gwt.entity.GwtGameDbInfo;
import com.boardgame.gwt.entity.GwtPlayerDbInfo;
import com.boardgame.gwt.enums.GameStatus;
import com.boardgame.gwt.enums.GameType;
import com.boardgame.gwt.repository.GwtGameInfoRepository;
import com.boardgame.gwt.repository.GwtPlayerRepository;
import com.boardgame.users.entity.PlayerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by pdybka on 16.05.16.
 */
@Service
@Transactional
public class GwtGameInfoService {

    @Autowired
    private GwtGameInfoRepository gameRepository;

    //@Autowired
    //private GwtPlayerRepository gwtPlayerRepository;

    @Autowired
    public GwtGameInfoService(GwtGameInfoRepository gameRepository) {
        this.gameRepository = gameRepository;
    }

    /**
     * 创建新游戏
     * @param player
     * @param gameDTO
     * @return
     */
    public GwtGameDbInfo createNewGame(GwtPlayerDbInfo player, GwtGameDbInfo gameDTO) {
        GwtGameDbInfo game = gameDTO;
//        game.setFirstPlayer((GwtPlayer)player);
//        //其他玩家
//        this.setPlayers(gameDTO);
        //状态
        game.setGameStatus(gameDTO.getGameType() == GameType.COMPUTER ? GameStatus.IN_PROGRESS :
                GameStatus.WAITS_FOR_PLAYER);
        //时间
        game.setCreated(new Date());
        gameRepository.save(game);
        return game;
    }


    /**
     * 更新游戏状态
     * @param game
     * @param gameStatus
     * @return
     */
    public GwtGameDbInfo updateGameStatus(GwtGameDbInfo game, GameStatus gameStatus) {
        GwtGameDbInfo g = getGame(game.getId());
        g.setGameStatus(gameStatus);

        return g;
    }

    /**
     * 加入的游戏
     * @param player
     * @return
     */
//    public List<GwtGameInfo> getGamesToJoin(PlayerInfo player) {
//        return gameRepository.findByGameTypeAndGameStatus(GameType.COMPETITION,
//                GameStatus.WAITS_FOR_PLAYER).stream().filter(game -> game.getFirstPlayer().getUserName() != player.getUserName()).collect(Collectors.toList());
//
//    }

    /**
     * 加入游戏
     * @param player
     * @param gameDTO
     * @return
     */
    public GwtGameDbInfo joinGame(GwtPlayerDbInfo player, GwtGameDbInfo gameDTO) {
        GwtGameDbInfo game =  getGame((long) gameDTO.getId());

        gameRepository.save(game);

        updateGameStatus(game, GameStatus.IN_PROGRESS);

        return game;

    }

    /**
     * 获取玩家游戏
     * @param player
     * @return
     */
//    public List<GwtGameInfo> getPlayerGames(PlayerInfo player) {
//        return gameRepository.findByGameStatus(
//                GameStatus.IN_PROGRESS).stream().filter(game -> game.getFirstPlayer().getUserName() == player.getUserName()).collect(Collectors.toList());
//    }

    /**
     * 根据id获取游戏
     * @param id
     * @return
     */
    public GwtGameDbInfo getGame(Long id) {
        return gameRepository.findById(id).get();
    }
    /**
     * 获取游戏详细
     * @param gameName
     * @return
     */
    public GwtGameDbInfo getGameByname(String gameName) {
        return gameRepository.findOneByGameName(gameName);
    }
}
